package cn.xinfei.xdecision.common.service.common;

import cn.xinfei.xdecision.common.model.datax.datamanage.request.DeriveVarSnapshotDetailVO;
import cn.xinfei.xdecision.common.model.datax.datamanage.request.DeriveVarSnapshotVO;
import cn.xinfei.xdecision.common.model.datax.datamanage.request.VariableSaveAndUpdateParam;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.Variable;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.VariableSnapshot;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.vo.VariableDTO;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.resp.VariableResp;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.vo.VariableRespVO;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.vo.VariableResumeVO;
import cn.xinfei.xdecision.common.model.datax.enums.EnumUtil;
import cn.xinfei.xdecision.common.model.datax.enums.VariableEnum;
import cn.xinfei.xdecision.common.model.datax.enums.VariableTypeEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class DataConvertUtil {

    public static VariableResp convertToVarResp(Variable variable) {
        VariableResp variableResp = new VariableResp();
        variableResp.setVarCode(variable.getVarCode());
        variableResp.setVarName(variable.getVarName());
        VariableEnum variableEnum = EnumUtil.fromCodeOrThrow(VariableEnum.class, String.valueOf(variable.getVarType()));
        variableResp.setVarType(variableEnum != null ? variableEnum.getCode() : null);
        VariableTypeEnum variableTypeEnum = EnumUtil.fromCodeOrThrow(VariableTypeEnum.class, String.valueOf(variable.getVarValueType()));
        variableResp.setValueType(variableTypeEnum != null ? variableTypeEnum.getCode() : null);
        variableResp.setDefaultValue(variable.getVarDefaultValue());
        return variableResp;
    }


    public static VariableDTO convertToVarDTO(Variable variable, String script, Set<String> paramVarCodes) {
        VariableDTO variableDto = new VariableDTO();
        variableDto.setVarCode(variable.getVarCode());
        variableDto.setVarName(variable.getVarName());
        variableDto.setVarType(EnumUtil.fromCodeOrThrow(VariableEnum.class, String.valueOf(variable.getVarType())).getDesc());
        variableDto.setValueType(EnumUtil.fromCodeOrThrow(VariableTypeEnum.class, String.valueOf(variable.getVarValueType())).name().toLowerCase());
        variableDto.setDefaultValue(variable.getVarDefaultValue());
        variableDto.setScript(script);
        variableDto.setParameterVariables(paramVarCodes);
        return variableDto;
    }

    public static VariableRespVO convertToVarVo(Variable variable, Set<String> dependentVars) {
        VariableRespVO variableRespVo = new VariableRespVO();
        variableRespVo.setVarCode(variable.getVarCode());
        variableRespVo.setVarName(variable.getVarName());
        variableRespVo.setVarType(String.valueOf(variable.getVarType()));
        variableRespVo.setVarValueType(String.valueOf(variable.getVarValueType()));
        variableRespVo.setVarDefaultValue(variable.getVarDefaultValue());
        variableRespVo.setDependentVars(dependentVars);
        return variableRespVo;
    }

    public static VariableSaveAndUpdateParam fromVariableResumeVO(VariableResumeVO variable, Long organId) {
        VariableSaveAndUpdateParam saveAndUpdateParam = new VariableSaveAndUpdateParam();
        saveAndUpdateParam.setVarCode(variable.getVarCode());
        saveAndUpdateParam.setVarName(variable.getVarName());
        saveAndUpdateParam.setVarType(variable.getVarType());
        saveAndUpdateParam.setValueType(variable.getValueType());
        saveAndUpdateParam.setVarDefaultValue(variable.getVarDefaultValue());
        saveAndUpdateParam.setScript(variable.getScript());
        saveAndUpdateParam.setParameterVariables(new ArrayList<>(variable.getParameterVariables()));
        saveAndUpdateParam.setFolderId(variable.getFolderId());
        saveAndUpdateParam.setOrganId(organId);
        saveAndUpdateParam.setModifier(variable.getModifier());
        saveAndUpdateParam.setOrganId(variable.getOrganId());
        return saveAndUpdateParam;
    }


    public static VariableSnapshot fromVarSnapshot(VariableSnapshot variableSnapshot, String copyVarCode, String snapshot) {
        VariableSnapshot snapshotVo = new VariableSnapshot();
        snapshotVo.setVarCode(copyVarCode);
        snapshotVo.setVersion(variableSnapshot.getVersion());
        snapshotVo.setUpdateTime(variableSnapshot.getUpdateTime());
        snapshotVo.setCreateTime(variableSnapshot.getCreateTime());
        snapshotVo.setModifier(variableSnapshot.getModifier());
        snapshotVo.setCreator(variableSnapshot.getCreator());
        snapshotVo.setOrganId(variableSnapshot.getOrganId());
        DeriveVarSnapshotVO deriveVarSnapshotVO = JSON.parseObject(snapshot, DeriveVarSnapshotVO.class);
        Optional.ofNullable(deriveVarSnapshotVO).ifPresent(var -> deriveVarSnapshotVO.setVarCode(copyVarCode));
        snapshotVo.setSnapshot(JSONObject.toJSONString(deriveVarSnapshotVO));
        return snapshotVo;
    }

    public static VariableSnapshot fromVariableSaveAndUpdateParam(VariableSaveAndUpdateParam saveAndUpdateParam, Long currentVersion) {

        VariableSnapshot variableSnapshot = new VariableSnapshot();
        variableSnapshot.setVarCode(saveAndUpdateParam.getVarCode());
        variableSnapshot.setVersion(Objects.isNull(currentVersion) ? 0L : currentVersion + 1);
        variableSnapshot.setCreator(saveAndUpdateParam.getCreator());
        variableSnapshot.setModifier(saveAndUpdateParam.getModifier());
        variableSnapshot.setOrganId(saveAndUpdateParam.getOrganId());
        DeriveVarSnapshotVO deriveVarSnapshotVO = DataConvertUtil.convertToDeriveVarSnapshotVO(saveAndUpdateParam);
        variableSnapshot.setSnapshot(JSONObject.toJSONString(deriveVarSnapshotVO));
        return variableSnapshot;
    }


    public static DeriveVarSnapshotVO convertToDeriveVarSnapshotVO(VariableSaveAndUpdateParam saveAndUpdateParam) {
        DeriveVarSnapshotVO deriveVarSnapshotVO = new DeriveVarSnapshotVO();
        deriveVarSnapshotVO.setVarCode(saveAndUpdateParam.getVarCode());
        deriveVarSnapshotVO.setVarType(saveAndUpdateParam.getVarType());
        deriveVarSnapshotVO.setFolderId(saveAndUpdateParam.getFolderId());
        deriveVarSnapshotVO.setParameterVariables(saveAndUpdateParam.getParameterVariables());
        deriveVarSnapshotVO.setScript(saveAndUpdateParam.getScript());
        deriveVarSnapshotVO.setValueType(saveAndUpdateParam.getValueType());
        deriveVarSnapshotVO.setCreator(saveAndUpdateParam.getCreator());
        deriveVarSnapshotVO.setModifier(saveAndUpdateParam.getModifier());
        deriveVarSnapshotVO.setDesc(saveAndUpdateParam.getDesc());
        deriveVarSnapshotVO.setFolderId(saveAndUpdateParam.getFolderId());
        deriveVarSnapshotVO.setDesc(saveAndUpdateParam.getDesc());
        deriveVarSnapshotVO.setVarDefaultValue(saveAndUpdateParam.getVarDefaultValue());
        deriveVarSnapshotVO.setVarName(saveAndUpdateParam.getVarName());
        deriveVarSnapshotVO.setOrganId(saveAndUpdateParam.getOrganId());
        return deriveVarSnapshotVO;
    }

    public static DeriveVarSnapshotDetailVO fromDeriveVarSnapshotVO(DeriveVarSnapshotVO deriveVarSnapshotVO) {
        DeriveVarSnapshotDetailVO deriveVarSnapshotDetailVO = new DeriveVarSnapshotDetailVO();
        deriveVarSnapshotDetailVO.setDesc(deriveVarSnapshotVO.getDesc());
        deriveVarSnapshotDetailVO.setModifier(deriveVarSnapshotVO.getModifier());
        deriveVarSnapshotDetailVO.setCreator(deriveVarSnapshotVO.getCreator());
        deriveVarSnapshotDetailVO.setVersionId(deriveVarSnapshotVO.getVersionId());
        deriveVarSnapshotDetailVO.setUpdateTime(deriveVarSnapshotVO.getUpdateTime());
        return deriveVarSnapshotDetailVO;
    }
}
